package com.java.web.manageweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.java.manage.pojo.TbContentCategory;
import com.java.manage.service.TbContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @ClassName ContentCategoryController
 * @Description 前台广告服务
 * @Author Trigger Wu
 * @Date 2018/9/1116:32
 * @Version 1.0
 **/
@RequestMapping("/rest/content/category")
@Controller
public class ContentCategoryController {
    @Autowired
    private TbContentCategoryService categoryService;

    /*
     *     根据父节点id查询内容分类列表
     *
     * @author Tigger Wu
     * @date 2018/9/11 16:41
     * @param [pid]
     * @return org.springframework.http.ResponseEntity<java.util.List<com.java.manage.pojo.TbContentCategory>>
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<TbContentCategory>> queryListByParentId(
            @RequestParam(value = "id", defaultValue = "0") Long pid
            ) {
        try {
            EntityWrapper<TbContentCategory> wrapper = new EntityWrapper<TbContentCategory>();
            wrapper.where("parent_id={0} and status !=2",pid);
            List<TbContentCategory> list = categoryService.selectList(wrapper);
            if (null == list || list.isEmpty()) {
                EntityWrapper<TbContentCategory> wrapper1 = new EntityWrapper<TbContentCategory>();
                wrapper1.where("id={0}");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /*
     *  新增节点
     * 
     * @author Tigger Wu
     * @date 2018/9/11 16:41
     * @param [contentCategory]
     * @return org.springframework.http.ResponseEntity<com.java.manage.pojo.TbContentCategory>
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<TbContentCategory> saveContentCategory(TbContentCategory contentCategory) {
        try {
            categoryService.saveNode(contentCategory);
            return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /*
     *  重命名
     * 
     * @author Tigger Wu
     * @date 2018/9/11 16:41
     * @param [id, name]
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> rename(@RequestParam("id") Long id, @RequestParam("name") String name) {
        try {
            TbContentCategory category = new TbContentCategory();
            category.setId(id);
            category.setName(name);
            categoryService.updateById(category);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除节点，包含它的所有子节点都被删除
     *
     * @param contentCategory
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> delete(TbContentCategory contentCategory) {
        try {
            //假删除，修改status状态
            categoryService.deleteAllNode(contentCategory);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
